<template>
  <div class="messageItem hover" @click="clickHandle" @mousedown="handleMousedown" @contextmenu.prevent>
    <div class="avatar_wrapper">
      <img :src="data.avatar" class="avatar" v-if="data.avatar">
      <div class="avatar border_black" v-else>
        {{ data.nick.substring(0, 1) }}
      </div>

    </div>
    <div class="detailed">
      <div class="detailed_main">
        <div class="nick">
          {{ data.nick }}
        </div>
        <div class="messageContent">
          {{ data.content }}
        </div>
      </div>
      <div class="detailed_other">
        <div class="time">
          {{ data.time }}
        </div>
        <div class="unreadCount" v-if="data.unreadCount">
          {{ (data.unreadCount > 99 ? '99+' : data.unreadCount) }}
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, type PropType } from 'vue';
import { useRouter } from 'vue-router';
const router = useRouter();

export interface MessageItemType {
  userId: number,
  nick: string;
  avatar?: string;
  content: string;
  time: string;
  unreadCount: number;
}

const emit = defineEmits(['update:modelValue', 'mousedown']);

const props = defineProps({
  modelValue: {
    type: Object as PropType<MessageItemType>,
    required: true
  },
})

const data = computed({
  get() {
    return props.modelValue;
  },
  set(val) {
    emit('update:modelValue', val);
  }
});

const handleMousedown = (e: MouseEvent) => {
  emit('mousedown', data.value, e);
}

const clickHandle = () => {
  data.value = { ...data.value, unreadCount: 0 };

  router.push({
    name: 'chatpannel',
    params: {
      id: props.modelValue?.userId
    }
  });
}
</script>

<style lang="scss" scoped>
.messageItem {
  height: 70px;
  box-sizing: border-box;
  display: flex;
  padding: 10px;
  cursor: pointer;
  user-select: none;
  background-color: rgb(255, 255, 255);
  transition: all 0.5s ease;

  &:hover {
    transition: all 0.3s ease;

  }

  .avatar_wrapper {
    display: flex;
    justify-content: center;
    align-items: center;

    .avatar {
      height: 45px;
      width: 45px;
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;
      user-select: none;
      object-fit: cover;
    }
  }

  .detailed {
    margin-left: 5px;
    display: flex;
    flex: 1;

    .detailed_main {
      display: flex;
      justify-content: center;
      flex-flow: column;
      flex: 1;
      min-width: 0;

      .nick {}

      .messageContent {
        opacity: 0.5;
        font-size: 12px;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        max-width: 200px;
      }
    }

    .detailed_other {
      font-size: 12px;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-flow: column;
      gap: 5px;
      user-select: none;

      .time {
        color: #999;
      }

      .unreadCount {
        background-color: rgb(244, 130, 130);
        min-width: 15px;
        padding: 0px 2px;
        border-radius: 35%;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    }
  }
}

.border_black {
  border: 1px solid black;
}
</style>
